package com.zky.controller;

import com.zky.pojo.User;
import com.zky.service.userService;
import com.zky.service.userServiceImp;
import com.zky.util.JwtHelper;
import com.zky.util.Result;
import com.zky.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@CrossOrigin
@RequestMapping("/user")
public class userController {

    @Autowired
    private userService serviceImp;
    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User user)
    {
        Result login = serviceImp.login(user);
        return login;
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(@RequestHeader String token)
    {
        return serviceImp.getUserInfo(token);
    }

    @PostMapping("/checkUserName")
    @ResponseBody
    public Result checkUserName(String username)
    {
        Result result = serviceImp.checkUserName(username);
        return result;
    }
    @PostMapping("/regist")
    @ResponseBody
    public Result regist(@RequestBody User user)
    {
        Result regist = serviceImp.regist(user);
        return regist;
    }
    @GetMapping("/checkLogin")
    @ResponseBody
    public Result checkLogin(@RequestHeader String token)
    {
        if(token==null||"".equals(token)|| jwtHelper.isExpiration(token))
        {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }
}
